jQuery(document).ready(function($){
	//-- Header --//
	 
    $('.user_options').click(function(){
    	$(this).siblings('.user_options_drop').show(50);
    	$(".user_options_close").show();
    	$("#down").hide();
    });
    
    $('.user_options_close').click(function(){
    	$(this).siblings('.user_options_drop').hide(200);
    	$("#down").show();
    	$(".user_options_close").hide();
    });
    //-- Header End --//
    //-- Footer --//
    
    /* Search */
    $(".searchbar").focus(function (){
      var value = $(".searchbar").val();
      if(value == 'Enter Brand, Product or Keyword'){
      $(".searchbar").val('');
      $('.searchbar').css({'color': 'black'});
      }
    });

    $('.searchbar').blur(function() {
      var value = $(".searchbar").val();
      if(value == ''){
      $('.searchbar').css({'color': 'grey'});
      $(".searchbar").val('Enter Brand, Product or Keyword');
      }
    });
    
    $("#searchform").submit(function(){
      var value = $(".searchbar").val();
      if(value == 'Enter Brand, Product or Keyword' || value == ''){
      alert('Please Enter a Search Term');
      return false;
      }
    });
    /* Search */
	
	/* Change City Div SlideDown */
    $('.change_city').click(function(){
    	$('#city_rest').slideToggle(500);
    });
    /* Change City Div SlideDown */


	/* Newsletter Signup */
    function isEmail(Mail) {
    	Mail=Mail.toLowerCase();
        return (Mail.search(/^[a-zA-Z]+([_\.-]?[a-zA-Z0-9]+)*@[a-zA-Z0-9]+([\.-]?[a-zA-Z0-9]+)*(\.[a-zA-Z]{2,4})+$/) != -1);
    }

    $(".newsletter_email").focus(function(){
        var value = $(this).val();
        if(value == 'Email Address'){
        	$(this).val('');
        }
      });

      $('.newsletter_email').blur(function() {
        var value = $(this).val();
        if(value == ''){
        	$(this).val('Email Address');
        }
      });


	function newsletter_ajax(email, city, nonce, form){
		$.ajax({ 
          	type: "post", 
          	url: buzzr_wp_url+"/wp-content/plugins/buzzr_newsletter/signup.php",
          	data: "newsletter_email="+email+"&newsletter_city="+city+"&ajax_nonce="+nonce+"&newsletter_signup="+form,
          	success: function(msg){
              		$(".newsletter_email").val('Email Address');
              		$("#newsletter_city option").attr('selected', false);
              		var obj = jQuery.parseJSON(msg);
              		if(obj.code === "1000"){
              			$(".newsletter_result").append(obj.message);
              			_gaq.push(['_trackEvent', 'Daily Newsletter', 'Subscribed', email]);
              		}else if(obj.code === "1001"){
              			$(".newsletter_result").append(obj.message);
              		}else if(obj.code === "1005"){
              			$(".newsletter_result").append(obj.message);
              		}else if(obj.code === "1004"){
              			$(".newsletter_result").append(obj.message);
              		}else if(obj.code === "1002"){
              			_gaq.push(['_trackEvent', 'Daily Newsletter', 'Subscribed', email]);
              			setTimeout( function() {
              				window.location = obj.url;
              			}, 1000 );	
              		}else if(obj.code === "1003"){
              			setTimeout( function() {
              				window.location = obj.url;
              			}, 1000 );	
              		}			
          	   }
        });

    	$('.loader').ajaxStart(function(){
		       $(this).show();
		    });
		    
	    $('.loader').ajaxStop(function(){
	      $(this).hide();
	    });
	}

	var err='';
	function newsletter_validation(email, city_id){
		if(email == 'Email Address'){
         err += 'Please Enter a Email Address\n';
         var error = 1;
        }
        else{
         if(isEmail(email) == 0){
           err += 'Email Address Entered is Invalid\n';
           var error = 1;
         }
        }

    	if(city_id == '--Choose City--'){
           err += 'Please Select a City';
           var error = 1;
        }
        return error;
	}
      
    $("#newsletter_signup").click(function(){
    	$(".newsletter_result").html('');
    	var newsletter_email = $("#newsletter_email").val();
    	var newsletter_city = $("#newsletter_city").val();
    	var newsletter_signup = $("#newsletter_signup").val();
    	var ajax_nonce = $("#ajax_nonce").val();
    	
    	var is_valid = newsletter_validation(newsletter_email, newsletter_city);

    	if(newsletter_signup == 'Sign Up'){
        	$("#result_footer").css('display','block');
        	$("#result_header").css('display','none'); 
        }
        
    	if(is_valid == 1){
           alert(err);
           err = '';
        }
        else{
        	newsletter_ajax(newsletter_email,newsletter_city, ajax_nonce, newsletter_signup);
        }
    });

    $("#signup_header").click(function(){
    	$(".newsletter_result").html('');
    	var newsletter_email = $("#email_header").val();
    	var newsletter_city = $("#city_header").val();
    	var newsletter_signup = $("#signup_header").val();
    	var ajax_nonce = $("#ajax_nonce_header").val();
    	
    	var is_valid = newsletter_validation(newsletter_email, newsletter_city);

    	if(newsletter_signup == 'Go'){
    		$("#result_footer").css('display','none');
        	$("#result_header").css('display','block');  
        }
        
    	if(is_valid == 1){
           alert(err);
           err = '';
        }
        else{
        	newsletter_ajax(newsletter_email,newsletter_city, ajax_nonce, newsletter_signup);
        }
    });
    /* Newsletter Signup */
	
});
